﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Framework;
using NLBS.Validate;

namespace NLBS.Views
{
   public partial class GuestBookPresenter: Presenter<IGuestBookView,IAction<GuestBook>>
    {
       public GuestBookPresenter() { }
      

       public override void OnViewLoaded()
       {
           if (BlogInfo.Instance().EnableGuestBook == 0)
           {
               return;
           }
           View.EnableGBook = true;

           this.OnSelecting(new GuestBookSelectEventArgs(null));

           List<GuestBook> lists = GuestBookData.GetPageGuestBook();

           if (!string.IsNullOrEmpty(View.q))
           {
               List<GuestBook> searchList = lists.FindAll(delegate(GuestBook c)
               {
                   return c.Content.Contains(View.q);
               });
               View.GBookList = searchList;
               return;
           }

           View.GBookList = lists;

       }

       public override void OnActionLoaded()
       {
           this.Action.DataSelectSingle += new EventHandler(Action_DataSelectSingle);
           this.Action.DataInsert += new EventHandler(Action_DataInsert);
           this.Action.DataUpdate += new EventHandler(Action_DataUpdate);
           this.Action.DataDelete += new EventHandler(Action_DataDelete);
           base.OnActionLoaded();
       }

       void Action_DataDelete(object sender, EventArgs e)
       {
           this.Action.Entity = GuestBookData.GetGuestBookByID(this.Action.keyID);
           OnDeleteing(new GuestBookDeleteEventArgs(this.Action.Entity));
           if (BlogInfo.Instance().CurrentUser.Group.Power.Delete < 1) throw new Exception("没有权限删除留言");
           GuestBookData.DeleteGuestBook(this.Action.keyID);
           OnDeleted(new GuestBookDeleteEventArgs(this.Action.Entity));
       }

       void Action_DataSelectSingle(object sender, EventArgs e)
       {
           this.Action.Entity = GuestBookData.GetGuestBookByID(this.Action.keyID);
       }

       void Action_DataUpdate(object sender, EventArgs e)
       {
           GuestBook gb=this.Action.Entity;
           OnUpdateing(new GuestBookUpdateEventArgs(gb));
           gb.EditMark = BlogInfo.Instance().CurrentUser.UserName + "$|$" + DateTime.Now.ToString();
           GuestBookData.UpdateGuestBookByID(this.Action.Entity);
           OnUpdated(new GuestBookUpdateEventArgs(gb));
       }

       void Action_DataInsert(object sender, EventArgs e)
       {
           OnInserting(new GuestBookInsertEventArgs(this.Action.Entity));
           
           GuestBookData.InsertGuestBook(this.Action.Entity);
           ValidateHelper.checkGBookPostDuration();
           OnInserted(new GuestBookInsertEventArgs(this.Action.Entity));
       }
    }
}
